package com.iplant.ams.controller.ams;

import com.iplant.ams.service.AMSService;
import com.iplant.ams.service.po.ams.AMSReceiveGoodsItem;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/AMSReceiveGoodsItem")
public class AMSReceiveGoodsItemController  extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(AMSReceiveGoodsItemController.class);

    @Autowired
    AMSService wAMSService;

    /**
     * 条件查询
     */
    @GetMapping("All")
    public Object All(HttpServletRequest request){
        Object wResult = new Object();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wReceiveID = StringUtils.parseInt(request.getParameter("ReceiveID"), -1);
            int wOrderItemID = StringUtils.parseInt(request.getParameter("OrderItemID"), -1);
            List<Integer> wStatusList = StringUtils.parseIntList(request.getParameter("StatusList"), ",");
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<AMSReceiveGoodsItem>> wServiceResult=wAMSService.AMS_GetAMSReceiveGoodsItemList(
                    wLoginUser, wReceiveID, wOrderItemID, wStatusList, -1, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        }catch (Exception ex){
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam){
        Object wResult = new Object();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            AMSReceiveGoodsItem wItem = CloneTool.Clone(wParam.get("data"), AMSReceiveGoodsItem.class);
            if (wItem == null || wItem.getReceiveID() <= 0) { //父级ID必填
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wAMSService.AMS_UpdateReceiveGoodsItem(wLoginUser, wItem);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }

    /**
     * 单条删除
     */
    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam){
        Object wResult = new Object();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 参数校验
            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取数据
            AMSReceiveGoodsItem wItem = CloneTool.Clone(wParam.get("data"), AMSReceiveGoodsItem.class);
            if (wItem == null || wItem.getID() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 删除
            ServiceResult<Integer> wServiceResult = wAMSService.AMS_DeleteReceiveGoodsItem(wLoginUser, wItem);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
            logger.error(ex.toString());
        }

        return wResult;
    }

    /**
     * 批量删除
     */
    @PostMapping("/DeleteList")
    public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam){
        Object wResult = new Object();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 参数校验
            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取数据
            List<AMSReceiveGoodsItem> wItemList=CloneTool.CloneArray(wParam.get("data"),AMSReceiveGoodsItem.class);
            if (wItemList == null || wItemList.isEmpty()) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 执行
            ServiceResult<Integer> wServiceResult = wAMSService.AMS_DeleteReceiveGoodsItemList(wLoginUser, wItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
            logger.error(ex.toString());
        }

        return wResult;
    }
}
